﻿/*
 
    This file is part of AnkhBG.

    AnkhBG is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AnkhBG is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with AnkhBG.  If not, see <http://www.gnu.org/licenses/>.
 
 */

using System;
using System.Windows.Forms;
using Glider.Common.Objects;

namespace AnkhRestart
{
    public class Main : IDisposable
    {
        private static Main _Instance = null;
        public static Main Instance
        {
            get
            {
                if (_Instance == null)
                    _Instance = new Main();
                return _Instance;
            }
        }
        private bool IsDisposed = false;

        public MainForm Window = new MainForm();
        public Runner Run = new Runner();

        public static string version = "0.1";

        public bool error = false;

        private bool _running = false;
        public bool running
        {
            get
            {
                return _running;
            }
            set
            {
                _running = value;
                if (value == true)
                {
                    error = false;

                    Window.Log("Started");

                    Window.StartStopButton.Image = global::AnkhRestart.Properties.Resources.control_stop_blue;
                    Window.StartStopButton.Text = "Stop";
                    Window.StartStopButton.ToolTipText = "Stop";
                    Window.toolStrip1.ResumeLayout(false);

                    Window.StatusText.Image = global::AnkhRestart.Properties.Resources.tick;
                    Window.StatusText.Text = "Running";

                    GContext.Main.StartGlide();

                    Run.Start();
                }
                else
                {
                    Run.Stop();

                    if (GContext.Main.IsGliding)
                        GContext.Main.KillAction("AnkhRestart stopped", false);

                    Window.Log("Stopped");

                    Window.StartStopButton.Image = global::AnkhRestart.Properties.Resources.control_play_blue;
                    Window.StartStopButton.Text = "Start";
                    Window.StartStopButton.ToolTipText = "Start";
                    Window.toolStrip1.ResumeLayout(false);

                    if (!error)
                    {
                        Window.StatusText.Image = global::AnkhRestart.Properties.Resources.tick;
                        Window.StatusText.Text = "Stopped";
                    }
                }
            }
        }


        public Main()
        {
            Window.Show();

            Window.Log("Welcome to AnkhRestart " + version + " - The simple Glider-Restarter");
        }

        public void Dispose()
        {
            Dispose(true);
        }

        protected void Dispose(bool Disposing)
        {
            if (!IsDisposed)
            {
                if (Disposing)
                {
                    // managed
                    Run.Stop();
                }
                // unmanaged
            }
        }

        ~Main()
        {
            Dispose(false);
        }
    }
}